<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="类别"></el-table-column>
      <el-table-column label="商品">
        <template slot-scope="scope">
          <el-button type="success" @click="tototo(scope.row.id)"
            >商品</el-button
          >
          <el-button @click="openputcate(scope.row)">更新</el-button>
          <el-button @click="delcate(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button @click="dialogFormVisible=true">添加</el-button>
    
    <el-dialog title="添加类别" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="类别名称">
          <el-input v-model="name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcate()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    
    <el-dialog title="更新类别" :visible.sync="dialogFormVisible1">
      <el-form>
        <el-form-item label="类别名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="putcate()"
          >确 定</el-button
        >

      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../common/api";

export default {
  name: "ThisIsHome",
  data() {
    return {
      tableData: "",
      name:'',
      dialogFormVisible:false,
      dialogFormVisible1:false,
      form:''
    };
  },
  props: {},

  components: {},

  created() {},

  mounted() {
    this.getdata();
  },

  methods: {
    getdata() {
      axios
        .get("user/cates/")
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    tototo(id) {
      this.$router.push({ path: "/goods", query: { id: id } });
    },
    addcate(){
      axios
        .post("user/cates/", {name:this.name})
        .then((res) => {
          console.log(res);
          if(res.data.code == 200){
            this.$message.success('类别添加成功')
            this.name = ''
            this.dialogFormVisible = false
            this.getdata()
          }else{
            this.$message.error(res.data.msg)
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    openputcate(forms){
      this.form = forms
      this.dialogFormVisible1 = true
    },
    putcate(){
      axios
        .put("user/cates/", this.form)
        .then((res) => {
          console.log(res);
          if(res.data.code == 200){
            this.$message.success(res.data.msg)
            this.form = ''
            this.dialogFormVisible1 = false
            this.getdata()
          }else{
            this.$message.error(res.data.msg)
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    delcate(id){
      axios
        .delete("user/cates/", {params:{id:id}})
        .then((res) => {
          console.log(res);
          this.getdata()
          
        })
        .catch((error) => {
          this.$message.error(error);
        });
    }
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {},
};
</script>

<style scoped>
* {
  margin: 0;
}
</style>
